package cn.wm.netty.http;

import cn.hutool.log.Log;
import cn.hutool.log.StaticLog;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class HttpServer {
    private static final Log log = StaticLog.get();
    //两个工作线程组
    private static final EventLoopGroup boss = new NioEventLoopGroup(1);//负责处理链接请求
    private static final EventLoopGroup worker = new NioEventLoopGroup();       //负责处理具体业务

    public static void start(int port) {
        try {
            //服务器端 启动引导类
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new HttpServerHandlerInitializer());
            //绑定端口进行，监听
            ChannelFuture sync = bootstrap.bind(port).sync();
            log.info("Server start... listen port [{}]",port);
            sync.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            log.info(e);
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        start(9999);
    }
}
